<template>
  <q-page class="cc-admin">
    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" separator="cell" :columns="columns" :data="list" row-key="id" :pagination.sync="pagination" :visible-columns="group" @request="query" :rows-per-page-options="[10, 20, 50, 100]" selection="multiple" :selected.sync="selected">
        <template v-slot:top="">
          <div class="row no-wrap full-width">
            <q-input clearable outlined dense placeholder="请输入关键字搜索" class="on-left" @input="query" debounce="500" v-model="key">
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
            <div class="q-gutter-xs">
              <q-btn icon="add" no-wrap color="primary" label="新建" @click="add" />
            </div>
          </div>
        </template>

        <template #body-cell-questionStem="props">
          <q-td :props="props" :auto-width="true" style="min-width: 800px; max-width: 800px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">
            {{ props.row.questionStem }}
          </q-td>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="edit" @click="edit(props.row)"> <q-tooltip>编辑</q-tooltip></q-btn>
            <q-btn flat round dense color="primary" icon="mdi-content-copy" @click="copy(props.row)">
              <q-tooltip>复制</q-tooltip>
            </q-btn>
            <btn-del label="简答题" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{ editType }}八选五
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-6">
              <h5>题干：</h5>
              <!-- <q-input outlined dense v-model="form.questionStem" type="textarea" /> -->
              <q-editor v-model="form.questionStem" min-height="400px" :content-style="{ 'max-height': '600px' }" :definitions="{
                bold: { label: 'Bold', icon: null, tip: 'My bold tooltip' },
              }" />
            </div>

            <!-- 出题选项卡------------------------------------------------------------------------ -->
            <div class="col-6" style="height: 100%">
              <graph @orderNode="orderNode" :orderNode="form.orderNode"></graph>
            </div>
          </div>
          <div class="row" style="min-height: 200px;width: 100%;">
            <div class="col-6 q-px-md">
              <span>答案一：</span>
              <q-input outlined dense v-model="form.answer1" type="text" />
            </div>
            <div class="col-6 q-px-md">
              <span>答案二：</span>
              <q-input outlined dense v-model="form.answer2" type="text" />
            </div>
            <div class="col-6 q-px-md">
              <span>答案三：</span>
              <q-input outlined dense v-model="form.answer3" type="text" />
            </div>
            <div class="col-6 q-px-md">
              <span>答案四：</span>
              <q-input outlined dense v-model="form.answer4" type="text" />
            </div>
            <div class="col-6 q-px-md">
              <span>答案五：</span>
              <q-input outlined dense v-model="form.answer5" type="text" />
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup @click="reset" />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import graph from './graph.vue';

export default {
  name: 'EngBlankQuestion',
  mixins: [IndexMixin],
  components: {
    confirm,
    graph,
  },
  data() {
    return {
      catalog: '03',
      type: '3',
      index: 0,
      tab: 0,
      fileMd5: '',
      showVideo: false,
      fileResult: '',
      headers: [
        {
          name: 'Authorization',
          value: localStorage.getItem('Authorization'), // 从localStorage中正确获取值
        },
      ],
      form: {
        questionStem: '',
        type: '3',
        subjectId: '03',
        subjectText: '英语',
        majorId: '03',
        majorText: '英语',
        answer: [],
        orderNode: [],
      },
      emptyForm: {
        questionStem: '',
        type: '3',
        subjectId: '03',
        subjectText: '英语',
        majorId: '03',
        majorText: '英语',
        answer: [],
        orderNode: [],
      },
      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
        },
        {
          name: 'questionStem',
          align: 'left',
          label: '题目',
          field: 'questionStem',
        },
        {
          name: 'opt',
          align: 'center',
          label: '操作',
          field: 'id',
        },
      ],
      showQuery: true,
      // uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/select/five/list',
        add: '/select/five/add',
        edit: '/select/five/edit',
        copy: '/select/five/copy',
        delete: '/select/five/delete',
        deleteBatch: '/select/five/deleteBatch',
        exportXlsUrl: '/select/five/exportXls',
        importExcelUrl: '/select/five/importExcel',
      },
    };
  },
  methods: {
    orderNode(orderNode) {
      this.form.orderNode = orderNode;
    },
    // 使用JSON.parse和JSON.stringify
    reset() {
      this.index = 0;
      this.form = JSON.parse(JSON.stringify(this.emptyForm));
    },
    getDictLabel,
    initDict() { },
    uploadResult(info) {
      const fileResult = JSON.parse(info.xhr.response);
      this.fileResult = fileResult;
      console.log(this.index);
      this.form.questionList[this.index].viedoUrl = `http://127.0.0.1:9000${fileResult.url}`;

      this.showVideo = true;
    },
    delVideo() {
      console.log(this.form.questionList);
      console.log(this.index);
      this.$axios
        .post(`${process.env.SERVER_URL}${process.env.BASE_URL}/deleteVideo`, {
          url: this.form.questionList[this.index].viedoUrl,
        })
        .then((r) => {
          this.$info(r.module);
          this.form.questionList[this.index].viedoUrl = '';
        });
    },
    query(props) {
      if (!this.beforeQuery()) {
        return false;
      }
      if (props && props.pagination) {
        this.pagination = props.pagination;
      }
      this.loading = true;
      return this.$axios
        .get(this.url.list, {
          params: {
            ...this.queryParam(),
            ...this.searchForm,
            key: this.key,
            type: this.type,
            catalog: this.catalog,
            pageNo: this.pagination.page,
            pageSize: this.pagination.rowsPerPage,
          },
        })
        .then((r) => {
          if (!r.result || !r.result.records || r.result.records.length === 0) {
            this.list = [];
            this.pagination.rowsNumber = 0;
          } else {
            this.list = r.result.records.map((v, i) => {
              if (r.result.current > 1) {
                v.index = (r.result.current - 1) * r.result.size + (i + 1);
              } else {
                v.index = i + 1;
              }
              return v;
            });
            this.pagination.rowsNumber = r.result.total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
  mounted() { },
};
</script>

<style lang="stylus">
label
  span
    margin-top 10px
img
  width 100%
  height auto
  aspect-ratio 16/9
.q-splitter__before, .q-splitter__after
    display inline-table
</style>
